import { userAccountLogin } from '@/api/user.js'

// 用户模块
export default {
  namespaced: true,
  state: () => ({
    // 用户信息
    User: {
      id: '',
      account: '',
      nickname: '',
      avatar: '',
      token: '',
      mobile: ''
    }
  }),
  mutations: {
    setUser(state, payload) {
      state.User = payload
    },
    // 清空用户信息
    clearUser(state, payload) {
      state.User = {}
      // 不能写 null null会使User.token 报错
    }
  },
  actions: {
    async asyncSetUser(ctx, payload) {
      const res = await userAccountLogin(payload)
      ctx.commit('setUser', res.result)
    }
  }
}
